#include "addfriend.h"
#include "ui_addfriend.h"
#include "login.h"
#include "protocol.h"
#include<QMessageBox>

addfriend::addfriend(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addfriend)
{
    ui->setupUi(this);

}

addfriend::~addfriend()
{
    delete ui;
}

void addfriend::setOnlineUsers(PDU *pdu)
{
    if(NULL == pdu)
    {
        return ;
    }
    uint uiSize = pdu -> uiMsgLen / 32;
    char caTmp[32];

    ui -> listWidget -> clear();
    for(uint i = 0; i < uiSize; ++ i)
    {
        memcpy(caTmp, (char*)(pdu -> caMsg) + 32 * i, 32);
        if(strcmp(caTmp, login::getInstance().getStrName().toStdString().c_str()) == 0)
        {
            continue;
        }
        ui -> listWidget -> addItem(caTmp);
    }

}


void addfriend::on_addFriendPB_clicked()
{
    QListWidgetItem *pItem = ui -> listWidget -> currentItem();
    if(NULL == pItem)
    {
        QMessageBox::warning(this, "添加好友", "请选择要添加的好友！");
        return ;
    }

    QString strAddName = pItem->text();
    QString strLoginName = login::getInstance().getStrName();
    PDU* pdu = mkPDU(0);

    qDebug() << "on_addfriend_pb_clicked  " << strAddName << " " << strLoginName;
    pdu -> uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_REQUEST;
    memcpy(pdu->caData, strAddName.toStdString().c_str(), strAddName.size());
    memcpy(pdu->caData + 32, strLoginName.toStdString().c_str(), strLoginName.size());
    login::getInstance().getTcpSocket().write((char*)pdu, pdu -> uiPDULen);
    free(pdu);
    pdu = NULL;

}


